/*
 * @(#)MailUtil.java   1.0  2022年1月11日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.util;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;
import com.swift.exception.extend.SystemException;

/**
 * 添加说明
 * 
 * @author zhengjiajin
 * @version 1.0 2022年1月11日
 */
public class MailUtil {

    public static class MainSendUser {
        private String mailHost;
        
        private String sendMail;
        
        private String sendPassword;
        
        private String toMain;

        /**
         * @return the mailHost
         */
        public String getMailHost() {
            return mailHost;
        }

        /**
         * @param mailHost the mailHost to set
         */
        public void setMailHost(String mailHost) {
            this.mailHost = mailHost;
        }

        /**
         * @return the sendMail
         */
        public String getSendMail() {
            return sendMail;
        }

        /**
         * @param sendMail the sendMail to set
         */
        public void setSendMail(String sendMail) {
            this.sendMail = sendMail;
        }

        /**
         * @return the sendPassword
         */
        public String getSendPassword() {
            return sendPassword;
        }

        /**
         * @param sendPassword the sendPassword to set
         */
        public void setSendPassword(String sendPassword) {
            this.sendPassword = sendPassword;
        }

        /**
         * @return the toMain
         */
        public String getToMain() {
            return toMain;
        }

        /**
         * @param toMain the toMain to set
         */
        public void setToMain(String toMain) {
            this.toMain = toMain;
        }
        
    }
    
    public static class MainContent {
        
        private String title;
        
        private String content;

        /**
         * @return the title
         */
        public String getTitle() {
            return title;
        }

        /**
         * @param title the title to set
         */
        public void setTitle(String title) {
            this.title = title;
        }

        /**
         * @return the content
         */
        public String getContent() {
            return content;
        }

        /**
         * @param content the content to set
         */
        public void setContent(String content) {
            this.content = content;
        }
        
    }
    
    private static enum MailProtocolEnum {
        smtp,pop3
    }
    
    private static String mailProtocol(String mailHost) {
        for(MailProtocolEnum protocol:MailProtocolEnum.values()) {
            if(mailHost.toLowerCase().startsWith(protocol.name())) return protocol.name();
        }
        return "smtp";
    }
    
    public static void sendMain(MainSendUser user, MainContent content) {
        try {
            Properties properties = new Properties();
            properties.setProperty("mail.host", user.getMailHost());
            properties.setProperty("mail.transport.protocol", mailProtocol(user.getMailHost()));
            properties.setProperty("mail.smtp.auth", "true");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            // 创建一个session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user.getSendMail(), user.getSendPassword());
                }
            });
            session.setDebug(false);
            // 获取连接对象
            Transport transport = session.getTransport();
            transport.connect(user.getMailHost(), user.getSendMail(), user.getSendPassword());
            MimeMessage mimeMessage = createMimeMessage(session, user, content);
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
            transport.close();
        }catch(Exception ex) {
            throw new SystemException("发送邮件失败:",ex);
        }
    }
    
    private static MimeMessage createMimeMessage(Session session,MainSendUser user,MainContent content) throws Exception {
        // 创建邮件对象
        MimeMessage mimeMessage = new MimeMessage(session);
        // 邮件发送人
        mimeMessage.setFrom(new InternetAddress(user.getSendMail()));
        // 邮件接收人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getToMain()));
        mimeMessage.setSubject(content.getTitle());
        // 邮件内容
        mimeMessage.setContent(content.getContent(), "text/html;charset=UTF-8");
        return mimeMessage;
    }
    
}
